package studyOwn;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SwingComponentDemo {
    JFrame f = new JFrame("basic ");

    //声明菜单相关组件
    JMenuBar menuBar = new JMenuBar();

    JMenu fileMenu = new JMenu("file");
    JMenu editMenu = new JMenu("edit");

    JMenuItem auto = new JMenu("enter");
    JMenuItem copy = new JMenu("copy");
    JMenuItem paste = new JMenu("paste");

    JMenu formatMenu = new JMenu("format");
    JMenuItem comment = new JMenu("comment");
    JMenuItem cancelComment = new JMenu("cancelComment");

    //声明文本于
    JTextArea ja = new JTextArea(8, 20);

    String[] color = {"red", "blue", "yellow"};
    // 声明列表框
    JList<String> colorList = new JList<String>(color);

    // 声明选择框
    JComboBox<String> colorSelect = new JComboBox<>();

    JRadioButton male = new JRadioButton("male",false);
    JRadioButton female = new JRadioButton("female",true);

    JCheckBox isMarried = new JCheckBox("isMarried",true);

    //声明底部
    JTextField jf = new JTextField(40);
    JButton ok = new JButton("sure");

    //声明右键彩单
    JPopupMenu jPopupMenu = new JPopupMenu();

    ButtonGroup buttonGroup = new ButtonGroup();
    JRadioButton metal = new JRadioButton("metal");
    JRadioButton nimbus = new JRadioButton("nimbus");
    JRadioButton windows = new JRadioButton("windows",true);
    JRadioButton windowsClassic = new JRadioButton("windowsClassic");
    JRadioButton  motif = new JRadioButton("motif");


    // 初始化
    public void init(){

        //组装视图

        //组装底部
        JPanel bottomPanel = new JPanel();
        bottomPanel.add(jf);
        bottomPanel.add(ok);
        f.add(bottomPanel, BorderLayout.NORTH);

        //组装选择相关
        JPanel selectPanel = new JPanel();
        colorSelect.addItem("red");
        colorSelect.addItem("blue");
        colorSelect.addItem("yellow");
        selectPanel.add(colorSelect);
        buttonGroup.add(male);
        buttonGroup.add(female);
        selectPanel.add(male);
        selectPanel.add(female);
        selectPanel.add(isMarried);

        //组装文本域耳环选择相关组件
        Box topLeft = Box.createVerticalBox();
        topLeft.add(ja);
        topLeft.add(selectPanel);

        //组装顶部
        Box top = Box.createHorizontalBox();
        top.add(topLeft);
        top.add(colorList);

        f.add(top);

        //组装顶部菜单
        formatMenu.add(comment);
        formatMenu.add(cancelComment);

        editMenu.add(auto);
        editMenu.addSeparator();
        editMenu.add(copy);
        editMenu.add(paste);
        editMenu.addSeparator();
        editMenu.add(formatMenu);

        menuBar.add(fileMenu);
        menuBar.add(editMenu);

        f.setJMenuBar(menuBar);

        //组装右击
        buttonGroup.add(metal);
        buttonGroup.add(nimbus);
        buttonGroup.add(windows);
        buttonGroup.add(windowsClassic);
        buttonGroup.add(motif);

        ActionListener listener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String actionCommand = e.getActionCommand();
                try {
                    changeFlavor(actionCommand);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        };

        metal.addActionListener(listener);
        nimbus.addActionListener(listener);
        windows.addActionListener(listener);
        windowsClassic.addActionListener(listener);
        motif.addActionListener(listener);

        jPopupMenu.add(metal);
        jPopupMenu.add(nimbus);
        jPopupMenu.add(windowsClassic);
        jPopupMenu.add(windows);
        jPopupMenu.add(motif);

        ja.setComponentPopupMenu(jPopupMenu);


        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
    }

    public static void main(String[] args) {
        new SwingComponentDemo().init();
    }

    //定义一个方法用于改变页面风格
    private void changeFlavor(String command)throws Exception{
        switch (command) {
            case "metal":
                UIManager.setLookAndFeel("java.swing.plaf.metal.MetalLookAndFeel");
                break;
            case "nimbus":
                UIManager.setLookAndFeel("java.swing.plaf.metal.MetalLookAndFeel");
                break;
            case "windows":
                UIManager.setLookAndFeel("java.swing.plaf.metal.MetalLookAndFeel");
                break;
            case "windowsClassic":
                UIManager.setLookAndFeel("java.swing.plaf.metal.MetalLookAndFeel");
                break;
            case "motif":
                UIManager.setLookAndFeel("java.swing.plaf.metal.MetalLookAndFeel");
                break;
        }

        SwingUtilities.updateComponentTreeUI(f.getContentPane());
        SwingUtilities.updateComponentTreeUI(menuBar);
        SwingUtilities.updateComponentTreeUI(jPopupMenu);
    }

}
